package com.sskj.fca.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.fca.R;
import com.sskj.fca.bean.WithDrawBean;
import com.sskj.fca.presenter.WithdrawRecordFragmentPresenter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.APP_FRAGMENT_WITHDRAW_RECORD)//提现记录
public class WithdrawRecordFragment extends BaseFragment<WithdrawRecordFragmentPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private MVCCoolHelper<List<WithDrawBean>> mvcHelper;


    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_withdraw_record;
    }

    @Override
    public WithdrawRecordFragmentPresenter getPresenter() {
        return new WithdrawRecordFragmentPresenter();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_withdraw, new SlimInjector<WithDrawBean>() {
            @Override
            public void onInject(WithDrawBean data, IViewInjector injector, List payloads) {
                if (data.getPay_type().equals("1")) {
                    BigDecimal bigUsdt = new BigDecimal(data.getPrice());
                    BigDecimal bigRate = new BigDecimal(data.getUsdt_rate());
                    injector.text(R.id.tv_withdraw_number, NumberUtil.keep4(String.valueOf(bigUsdt.multiply(bigRate))) + " CNY");
                } else if (data.getPay_type().equals("2")) {
                    injector.text(R.id.tv_withdraw_number, data.getPrice() + " USDT");
                }
                injector.text(R.id.tv_withdraw_address, data.getOrdername())
                        .text(R.id.tv_withdraw_start_date, data.getCreate_time())
                        .text(R.id.tv_withdraw_end_date, data.getShenhe_time())
                        .text(R.id.tv_fail_reason, data.getReason())
                        .text(R.id.tv_withdraw_state, data.getStatus().equals("0") ?
                                "待审核" : data.getStatus().equals("1") ?
                                "已同意" : data.getStatus().equals("2") ?
                                "已拒绝" : "")
                        .visibility(R.id.tv_fail_reason, data.getStatus().equals("2") ? View.VISIBLE : View.GONE);
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
//        ModelRx2DataSource<String> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<String>)
//                page -> Flowable.range(0, 20)
//                        .map(i -> i + "")
//                        .toList().toFlowable(), 20);
        ModelRx2DataSource<WithDrawBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<WithDrawBean>)
                        page -> mPresenter.getWithDrawList(page, 10), 10);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }
}
